package com.itheima.thread.threadPool.executorService;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @Author: Ethan Yankang
 * @Program: Interview
 * @Date: 2024-08-15 19:28
 **/
public class newSingleThreadExecutor_my {

        public static void main(String[] args) {
            test1();
            test2();
        }

        //练习方法newSingleThreadPool
        private static void test1() {
            //1:使用工厂类获取线程池对象
            ExecutorService es = Executors.newSingleThreadExecutor();
            //2:提交任务;
            for (int i = 1; i <= 10; i++) {
                es.submit(new MyRunnable(i));
            }
        }

        private static void test2() {
            //1:使用工厂类获取线程池对象
            ExecutorService es = Executors.newSingleThreadExecutor(new ThreadFactory() {
                int n = 1;

                @Override
                public Thread newThread(Runnable r) {
                    return new Thread(r, "自定义的线程名称" + n++);
                }
            });
            //2:提交任务;
            for (int i = 1; i <= 10; i++) {
                es.submit(new MyRunnable(i));

            }
        }
    }
